package com.yanfan.zutai.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.yanfan.common.core.domain.entity.SysDictData;
import com.yanfan.common.core.domain.model.LoginUser;
import com.yanfan.common.core.redis.RedisCache;
import com.yanfan.common.core.text.Convert;


import com.yanfan.common.utils.DateUtils;
import com.yanfan.common.utils.SecurityUtils;
import com.yanfan.zutai.domain.*;
import com.yanfan.zutai.domain.BDeviceParam;
import com.yanfan.zutai.domain.vo.*;
import com.yanfan.zutai.mapper.*;
import com.yanfan.zutai.service.*;
import com.yanfan.zutai.util.DateUtilLocal;
import com.yanfan.zutai.util.Detect;
import com.yanfan.zutai.util.EntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.*;

/**
 * 设备管理Service业务层处理
 *
 * @author ruoyi
 * @date 2021-02-27
 */
@Service
public class BDeviceManagerServiceImpl implements IBDeviceManagerService {
    @Autowired
    private BDeviceManagerMapper bDeviceManagerMapper;
    @Autowired
    private BCommonMapper bCommonMapper;
    @Autowired
    private BCommonService bCommonService;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private ISysUploadService sysUploadService;
    @Autowired
    private BDeviceRealDataMapper bDeviceRealDataService;
    @Autowired
    private IBDeviceWarnConfigService bDeviceWarnConfigService;
    @Autowired
    private IBDeviceParameterService bDeviceParameterService;
    @Autowired
    private IBDevicePhoneService bDevicePhoneService;
    @Autowired
    private IBDeviceZtService bDeviceZtService;
    @Autowired
    private IBDeviceZtBindService bDeviceZtBindService;
    @Autowired
    private BDeviceDriveMapper bDeviceDriveService;
    @Autowired
    private BDeviceComConfigMapper bDeviceComConfigService;
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private BDeviceZtBindMapper bDeviceZtBindMapper;


    @Override
    public BDeviceMonitorVo getDevice(BDeviceManager bDeviceManager) {
        BDeviceMonitorVo bDeviceMonitorVo = new BDeviceMonitorVo();
        // 设备总数
        Integer deviceSumNumber = 0;
        // 在线总数
        Integer onLineNumber = 0;
        // 离线数量
        Integer offLineNumber = 0;
        // 报警总数
        Integer warnNumber = 0;

        // 报警总个数
        Long warnRecordNum = 0L;
        // 报警已处理个数
        Long warnRecordProcessedNum = 0L;
        // 报警未处理个数
        Long warnRecordUntreatedNum = 0L;

        // 工单总个数
        Long orderRecordNum = 0L;
        // 工单已处理个数
        Long orderRecordProcessedNum = 0L;
        // 工单未处理个数
        Long orderRecordUntreatedNum = 0L;
        // 工单已废弃个数
        Long orderRecordAbandonNum = 0L;

        BDeviceZtBind bDeviceZtBind = new BDeviceZtBind();
        bDeviceZtBind.setZtGuid(bDeviceManager.getGuid());
        //总数量
        List<BDeviceZtBind> bDeviceZtBinds = bDeviceZtBindMapper.selectBDeviceZtBindList(bDeviceZtBind);
        deviceSumNumber = bDeviceZtBinds.size();
        // 在线数量
        bDeviceZtBind.setStatus("3");
        List<BDeviceZtBind> bDeviceZtBinds1 = bDeviceZtBindMapper.selectBDeviceZtBindListByztGuid(bDeviceZtBind);
        onLineNumber = bDeviceZtBinds1.size();

        // 离线数量
        bDeviceZtBind.setStatus("4");
        List<BDeviceZtBind> bDeviceZtBinds2 = bDeviceZtBindMapper.selectBDeviceZtBindListByztGuid(bDeviceZtBind);
        offLineNumber = bDeviceZtBinds2.size();

        // 告警数量
        warnNumber = bDeviceZtBindMapper.selectBDeviceZtBindCountByztGuid(bDeviceZtBind);

        bDeviceMonitorVo.setOffLineNumber(offLineNumber);
        bDeviceMonitorVo.setOnLineNumber(onLineNumber);
        bDeviceMonitorVo.setDeviceSumNumber(deviceSumNumber);
        bDeviceMonitorVo.setWarnNumber(warnNumber);

        return bDeviceMonitorVo;
    }



    /**
     * 查询设备管理
     *
     * @param id 设备管理ID
     * @return 设备管理
     */
    @Override
    public BDeviceManager selectBDeviceManagerById(Long id) {
        return bDeviceManagerMapper.selectBDeviceManagerById(id);
    }

    /**
     * 查询设备管理列表
     *
     * @param bDeviceManager 设备管理
     * @return 设备管理
     */
    @Override
    public List<BDeviceManager> selectBDeviceManagerList(BDeviceManager bDeviceManager) {
        return bDeviceManagerMapper.selectBDeviceManagerList(bDeviceManager);
    }

    /**
     * 新增设备管理
     * //参数1为终端ID
     * //参数2为数据中心ID
     *
     * @param bDeviceManager 设备管理
     * @return 结果
     */
    @Override
    public int insertBDeviceManager(BDeviceManager bDeviceManager) {
        bDeviceManager.setCreateTime(DateUtils.getNowDate());
        Snowflake snowflake = IdUtil.getSnowflake(6, 2);
        long id = snowflake.nextId();
        bDeviceManager.setId(id);
        return bDeviceManagerMapper.insertBDeviceManager(bDeviceManager);
    }

    /**
     * 修改设备管理
     *
     * @param bDeviceManager 设备管理
     * @return 结果
     */
    @Override
    public int updateBDeviceManager(BDeviceManager bDeviceManager) {
        return bDeviceManagerMapper.updateBDeviceManager(bDeviceManager);
    }

    /**
     * 批量删除设备管理
     *
     * @param ids 需要删除的设备管理ID
     * @return 结果
     */
    @Override
    public int deleteBDeviceManagerByIds(Long[] ids) {
        return bDeviceManagerMapper.deleteBDeviceManagerByIds(ids);
    }

    /**
     * 删除设备管理信息
     *
     * @param id 设备管理ID
     * @return 结果
     */
    @Override
    public int deleteBDeviceManagerById(Long id) {
        return bDeviceManagerMapper.deleteBDeviceManagerById(id);
    }


    @Override
    public BDeviceMonitorVo getDeviceOverview(BDeviceManager bDeviceManager) {
        BDeviceMonitorVo bDeviceMonitorVo = new BDeviceMonitorVo();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String dept = "";
        // TDOO lsy 2024.05.02
        /*if (loginUser.getUser().getDept().getIsDepartment() != null && loginUser.getUser().getDept().getIsDepartment() == 1) {
            dept = loginUser.getUser().getDept().getParentId() + "";
        } else {
            dept = loginUser.getUser().getDept().getDeptId() + "";
        }*/
        // 设备总数
        Integer deviceSumNumber = 0;
        // 在线总数
        Integer onLineNumber = 0;
        // 掉线总数
        Integer offLineNumber = 0;
        // 报警总数
        Integer warnNumber = 0;
        List<String> deviceNameList = new ArrayList<>();
        // 查询设备总数
        bDeviceManager.setDeptIdStrs(dept);
        List<BDeviceManager> sumList = this.selectBDeviceManagerList(bDeviceManager);
        for (int i = 0; i < sumList.size(); i++) {
            BDeviceManager deviceManager = sumList.get(i);
            deviceNameList.add(deviceManager.getDeviceName());
        }
        bDeviceMonitorVo.setDeviceSumNumber(sumList.size());
        bDeviceMonitorVo.setDeviceNameList(deviceNameList);
        // 查询离线数
        bDeviceManager.setDeviceStatus(0);
        List<BDeviceManager> offLineList = this.selectBDeviceManagerList(bDeviceManager);
        bDeviceMonitorVo.setOffLineNumber(offLineList.size());
        // 查询在线数
        bDeviceManager.setDeviceStatus(1);
        List<BDeviceManager> onLineList = this.selectBDeviceManagerList(bDeviceManager);
        bDeviceMonitorVo.setOnLineNumber(onLineList.size());
        // 查询报警数
        bDeviceManager.setDeviceStatus(2);
        List<BDeviceManager> warnList = this.selectBDeviceManagerList(bDeviceManager);
        bDeviceMonitorVo.setWarnNumber(warnList.size());
        return bDeviceMonitorVo;
    }

    @Override
    public String importData(List<DeviceExcelVo> deviceList) {
        int num = 0;
        String deviceMac = "";
        LoginUser loginUser = SecurityUtils.getLoginUser();
        for (int i = 0; i < deviceList.size(); i++) {
            DeviceExcelVo deviceExcelVo = deviceList.get(i);
            if (Detect.isEmpty(deviceExcelVo.getDeviceMac())) {
                continue;
            }
            deviceMac = deviceExcelVo.getDeviceMac();
            deviceMac = deviceMac.replace(" ", "");
            BDeviceManager bDeviceManager = new BDeviceManager();
            bDeviceManager.setDeviceMac(deviceMac);
            List<BDeviceManager> bDeviceManagerList = this.selectBDeviceManagerList(bDeviceManager);
            if (bDeviceManagerList.size() == 0) {
                bDeviceManager = new BDeviceManager();
                bDeviceManager.setDeviceMac(deviceExcelVo.getDeviceMac());
                bDeviceManager.setDeviceName(deviceExcelVo.getDeviceName());
                bDeviceManager.setDeviceType(deviceExcelVo.getDeviceType());
                bDeviceManager.setUpdateTime(new Date());
                bDeviceManager.setUpdateBy(loginUser.getUsername());
                bDeviceManager.setCreateTime(new Date());
                bDeviceManager.setCreateBy(loginUser.getUsername());
                bDeviceManager.setDeviceStatus(0);
                String deptIds = "," + loginUser.getUser().getDept().getAncestors() + "," + loginUser
                        .getUser()
                        .getDeptId() + ",";
                bDeviceManager.setDeptIdStrs(deptIds);
                bDeviceManager.setGuid(UUID.randomUUID().toString());
                this.insertBDeviceManager(bDeviceManager);
                this.createLog(bDeviceManager.getDeviceMac(), bDeviceManager.getDeviceName(), "设备导入");
                num++;
            }
        }
        return "成功导入" + num + "条设备数据！";
    }

    @Override
    public void createLog(String deviceMac, String deviceName, String operatingInstruction) {

    }

    @Override
    public BDeviceManager getParamList(BDeviceManager bDeviceManager) {
        List<BDeviceParam> bDeviceParamList = new ArrayList<>();
        if (Detect.notEmpty(bDeviceManager.getDeviceType())) {
            // 获取最新一条的实时记录
            BeanPropertyRowMapper<BDeviceHistory> bDeviceDetailMapper = new BeanPropertyRowMapper<>(BDeviceHistory.class);
            BDeviceHistory bDeviceHistory = new BDeviceHistory();
            String hisSql = "select * from b_device_history where device_mac=?  order by id desc limit 1";
            try {
                bDeviceHistory = jdbcTemplate.queryForObject(hisSql, bDeviceDetailMapper, bDeviceManager.getDeviceMac());// 处理结果集EmptyResultDataAccessException，不做任何操作
            } catch (EmptyResultDataAccessException e) {

            }
            BeanPropertyRowMapper<SysDictData> bDeviceMaintainRowMapper = new BeanPropertyRowMapper<>(SysDictData.class);
            String dictSql = "select  * from sys_dict_data where dict_type=? and status=0 order by dict_sort asc";
            List<SysDictData> sysDictDataList = jdbcTemplate.query(dictSql, bDeviceMaintainRowMapper, bDeviceManager.getDeviceType());
            Double paramRatio = null;
            for (int i = 0; i < sysDictDataList.size(); i++) {
                SysDictData sysDictData = sysDictDataList.get(i);
                BDeviceParam bDeviceParam = new BDeviceParam();
                Object paramValue = "无";
                Method getOrgIdMethod = null;
                Integer paramType = 0;
                if (sysDictData.getDictValue().indexOf("K") > -1) {
                    try {
                        getOrgIdMethod = bDeviceHistory
                                .getClass()
                                .getDeclaredMethod("getControl" + sysDictData.getDictValue());
                        paramValue = getOrgIdMethod.invoke(bDeviceHistory);
                        paramType = 1;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else if (sysDictData.getDictValue().indexOf("Z") > -1) {
                    try {
                        getOrgIdMethod = bDeviceHistory
                                .getClass()
                                .getDeclaredMethod("getState" + sysDictData.getDictValue());
                        paramValue = getOrgIdMethod.invoke(bDeviceHistory);
                        paramType = 2;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        paramRatio = Convert.toDouble(sysDictData.getIsDefault());
                        int length = 0;
                        if (paramRatio != null) {
                            length = sysDictData
                                    .getIsDefault()
                                    .substring(sysDictData.getIsDefault().indexOf('.') + 1)
                                    .length();
                        }
                        getOrgIdMethod = bDeviceHistory
                                .getClass()
                                .getDeclaredMethod("getAnalog" + sysDictData.getDictValue());
                        paramValue = getOrgIdMethod.invoke(bDeviceHistory);
                        paramType = 0;
                        if (paramRatio != null && paramValue != null) {
                            paramValue = (Double) paramValue * paramRatio;
                            paramValue = NumberUtil.round((Double) paramValue, length);
                        } else {
                            paramRatio = 1.0;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                // 判断是否报警
                /*if (Detect.notNull(paramValue) && sysDictData.getDictValue().indexOf("A") > -1) {
                    if (Detect.notNull(sysDictData.getWarnUpper()) && sysDictData.getWarnUpper() < Convert.toDouble(paramValue)) {
                        bDeviceParam.setIsUpWarn(true);
                    }
                    if (Detect.notNull(sysDictData.getWarnLower()) && sysDictData.getWarnLower() > Convert.toDouble(paramValue)) {
                        bDeviceParam.setIsLowerWarn(true);
                    }
                } else if (Detect.notNull(paramValue) && Detect.notNull(sysDictData.getWarnStatus()) && sysDictData.getDictValue().indexOf("Z") > -1
                        && paramValue.equals(sysDictData.getWarnStatus())) {
                    bDeviceParam.setIsStateWarn(true);
                }*/

                if (paramValue == null) {
                    paramValue = 0;
                }
                bDeviceParam.setIsDisplay("1".equals(sysDictData.getListClass()));
                bDeviceParam.setParamValue(paramValue);
                bDeviceParam.setParamField(sysDictData.getDictValue());
                bDeviceParam.setParamUnit(sysDictData.getRemark());
                bDeviceParam.setParamName(sysDictData.getDictLabel());
                bDeviceParam.setParamType(paramType);
                bDeviceParam.setParamRatio(paramRatio);
                if (Detect.notEmpty(sysDictData.getCssClass())) {
                    bDeviceParam.setImageUrl(sysDictData.getCssClass());
                } else {
                    bDeviceParam.setImageUrl("无");
                }
                bDeviceParamList.add(bDeviceParam);
            }
        }
        bDeviceManager.setParamList(bDeviceParamList);
        return bDeviceManager;
    }

    @Override
    public BDeviceEcharts getEcharts(BDeviceManager bDeviceManager) {
        List<BDeviceParam> bDeviceParamList = new ArrayList<>();
        BDeviceEcharts bDeviceEcharts = new BDeviceEcharts();
        BDeviceManager paramList = getParamList(bDeviceManager);
        bDeviceParamList = paramList.getParamList();
        List<String> paramFieldList = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        List<Double> paramRatioList = new ArrayList<>();
        Double paramRatio = null;
        for (int i = 0; i < bDeviceParamList.size(); i++) {
            BDeviceParam bDeviceParam = bDeviceParamList.get(i);
            if (bDeviceParam.getParamField().indexOf("A") > -1 && bDeviceParam.getIsDisplay()) {
                paramFieldList.add(bDeviceParam.getParamField());
                nameList.add(bDeviceParam.getParamName());
                paramRatioList.add(bDeviceParam.getParamRatio());
            }
        }
        if (paramFieldList.size() == 0) {
            return bDeviceEcharts;
        }
        String echartsSql = this.getEchartsSql(paramFieldList, bDeviceManager.getDeviceMac(), bDeviceManager.getQueryTime());
        BeanPropertyRowMapper<BDeviceEchartsData> bDeviceMaintainRowMapper = new BeanPropertyRowMapper<>(BDeviceEchartsData.class);
        List<BDeviceEchartsData> bDeviceEchartsDataList = jdbcTemplate.query(echartsSql, bDeviceMaintainRowMapper);
        Map<Integer, Object> map = new HashMap<>();
        for (int i = 0; i < bDeviceEchartsDataList.size(); i++) {
            BDeviceEchartsData bDeviceEchartsData = bDeviceEchartsDataList.get(i);
            map.put(bDeviceEchartsData.getTimeNum(), bDeviceEchartsData);
        }

        List<Integer> timeList = new ArrayList<>();
        List<List<Object>> anaList = new ArrayList<>();
        Method getOrgIdMethod = null;
        if (Detect.isEmpty(bDeviceManager.getQueryTime())) {
            bDeviceManager.setQueryTime(DateUtil.today());
        }
        Date parseDate = DateUtil.parseDate(bDeviceManager.getQueryTime());
        String fromatDate = DateUtil.formatDate(parseDate);
        int hour = 0;
        if (fromatDate.equals(DateUtil.today())) {
            hour = DateUtilLocal.getHour(new Date()) + 1;
        } else {
            hour = 24;
        }
        for (int j = 0; j < paramFieldList.size(); j++) {
            List<Object> list1 = new ArrayList<>();
            paramRatio = paramRatioList.get(j);
            for (int i = 0; i < hour; i++) {
                if (map.get(i) != null) {
                    BDeviceEchartsData bDeviceEchartsData = (BDeviceEchartsData) map.get(i);
                    try {
                        getOrgIdMethod = bDeviceEchartsData
                                .getClass()
                                .getDeclaredMethod("getAvgData" + paramFieldList.get(j));
                        double v = Convert.toDouble(getOrgIdMethod.invoke(bDeviceEchartsData)) * paramRatio;
                        list1.add(Convert.toInt(v));
                    } catch (Exception e) {
                        e.printStackTrace();
                        list1.add(0);
                    }
                } else {
                    list1.add(0);
                }
            }
            anaList.add(list1);
        }
        for (int i = 0; i < hour; i++) {
            timeList.add(i);
        }
        bDeviceEcharts.setNameList(nameList);
        bDeviceEcharts.setTimeList(timeList);
        bDeviceEcharts.setAnaList(anaList);
        return bDeviceEcharts;
    }

    @Override
    public BDeviceMonitorVo getDeviceOverviewWeChat(BDeviceManager bDeviceManager) {
        BDeviceMonitorVo bDeviceMonitorVo = new BDeviceMonitorVo();
        // 设备总数
        Integer deviceSumNumber = 0;
        // 在线总数
        Integer onLineNumber = 0;
        // 掉线总数
        Integer offLineNumber = 0;
        // 报警总数
        Integer warnNumber = 0;

        // 报警总个数
        Long warnRecordNum = 0L;
        // 报警已处理个数
        Long warnRecordProcessedNum = 0L;
        // 报警未处理个数
        Long warnRecordUntreatedNum = 0L;

        // 工单总个数
        Long orderRecordNum = 0L;
        // 工单已处理个数
        Long orderRecordProcessedNum = 0L;
        // 工单未处理个数
        Long orderRecordUntreatedNum = 0L;
        // 工单已废弃个数
        Long orderRecordAbandonNum = 0L;
        if ("查询工单".equals(bDeviceManager.getQueryType())) {
            orderRecordUntreatedNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=0 and YEAR(create_time)=YEAR(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            orderRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=2 and YEAR(create_time)=YEAR(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            orderRecordAbandonNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=3 and YEAR(create_time)=YEAR(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            orderRecordNum = orderRecordUntreatedNum + orderRecordProcessedNum + orderRecordAbandonNum;
        } else if ("查询报警".equals(bDeviceManager.getQueryType())) {
            // 报警统计
            warnRecordUntreatedNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where process_state=0 and YEAR(warn_time)=YEAR(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            warnRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where process_state=1 and YEAR(warn_time)=YEAR(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            warnRecordNum = warnRecordProcessedNum + warnRecordUntreatedNum;
        } else {
            // 设备统计
            offLineNumber = jdbcTemplate.queryForObject("select count(1) from B_Device_Manager where device_Status=0  and  dept_Id_strs like ?", Integer.class, bDeviceManager.getDeptIdStrs());
            onLineNumber = jdbcTemplate.queryForObject("select count(1) from B_Device_Manager where device_Status=1  and  dept_Id_strs like ?", Integer.class, bDeviceManager.getDeptIdStrs());
            warnNumber = jdbcTemplate.queryForObject("select count(1) from B_Device_Manager where device_Status=2  and  dept_Id_strs like ?", Integer.class, bDeviceManager.getDeptIdStrs());
            deviceSumNumber = offLineNumber + onLineNumber + warnNumber;

            // 报警统计
            warnRecordUntreatedNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where process_state=0  and  YEAR(warn_time)=YEAR(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            warnRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where process_state=1 and  YEAR(warn_time)=YEAR(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            warnRecordNum = warnRecordProcessedNum + warnRecordUntreatedNum;
            // 工单统计
            orderRecordUntreatedNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=0 and YEAR(create_time)=YEAR(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            orderRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=2 and YEAR(create_time)=YEAR(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            orderRecordAbandonNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=3 and YEAR(create_time)=YEAR(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            orderRecordNum = orderRecordUntreatedNum + orderRecordProcessedNum + orderRecordAbandonNum;
        }
        bDeviceMonitorVo.setOffLineNumber(offLineNumber);
        bDeviceMonitorVo.setOnLineNumber(onLineNumber);
        bDeviceMonitorVo.setWarnNumber(warnNumber);
        bDeviceMonitorVo.setDeviceSumNumber(deviceSumNumber);
        if (deviceSumNumber != 0) {
            bDeviceMonitorVo.setDeviceRate(NumberUtil.round((double) onLineNumber / deviceSumNumber, 2).doubleValue());
            bDeviceMonitorVo.setDeviceRateStr(NumberUtil
                                                      .round((double) onLineNumber / deviceSumNumber * 100, 2)
                                                      .doubleValue() + "%");
        } else {
            bDeviceMonitorVo.setDeviceRate(0.0);
            bDeviceMonitorVo.setDeviceRateStr("0%");
        }
        bDeviceMonitorVo.setWarnRecordUntreatedNum(warnRecordUntreatedNum);
        bDeviceMonitorVo.setWarnRecordProcessedNum(warnRecordProcessedNum);
        bDeviceMonitorVo.setWarnRecordNum(warnRecordNum);
        if (warnRecordNum != 0) {
            bDeviceMonitorVo.setWarnRate(NumberUtil
                                                 .round((double) warnRecordProcessedNum / warnRecordNum, 2)
                                                 .doubleValue());
            bDeviceMonitorVo.setWarnRateStr(NumberUtil
                                                    .round((double) warnRecordProcessedNum / warnRecordNum * 100, 2)
                                                    .doubleValue() + "%");
        } else {
            bDeviceMonitorVo.setWarnRate(0.0);
            bDeviceMonitorVo.setWarnRateStr("0%");
        }
        bDeviceMonitorVo.setOrderRecordUntreatedNum(orderRecordUntreatedNum);
        bDeviceMonitorVo.setOrderRecordProcessedNum(orderRecordProcessedNum);
        bDeviceMonitorVo.setOrderRecordNum(orderRecordNum);
        if (orderRecordNum != 0) {
            bDeviceMonitorVo.setOrderRate(NumberUtil
                                                  .round((double) orderRecordProcessedNum / orderRecordNum, 2)
                                                  .doubleValue());
            bDeviceMonitorVo.setOrderRateStr(NumberUtil
                                                     .round((double) orderRecordProcessedNum / orderRecordNum * 100, 2)
                                                     .doubleValue() + "%");
        } else {
            bDeviceMonitorVo.setOrderRate(0.0);
            bDeviceMonitorVo.setOrderRateStr("0%");
        }
        bDeviceMonitorVo.setOrderRecordAbandonNum(orderRecordAbandonNum);
        return bDeviceMonitorVo;
    }

    /*无此设备会返回null*/
    @Override
    public BDeviceManager selectBDeviceManagerByImei(String deviceImei) {
        if (Detect.isEmpty(deviceImei)) {
            return null;
        }
        return bDeviceManagerMapper.selectBDeviceManagerByImei(deviceImei);
    }

    @Override
    public Boolean selectBDeviceManagerOnlineByImei(String deviceImei) {
//        String url="http://81.68.197.219:18083/api/v5/clients/"+deviceImei;
//        BDeviceResult deviceStatus = HttpPostAndGetUtil.getDeviceStatus(url, "679111c378030e9f", "4n7vaBoYsq2Q33t9C1T2EW26P6Of4KuC39CMR9BEExEiJF");
        // return deviceStatus.getConnected();
//        BDeviceManager bDeviceManager = this.selectBDeviceManagerByImei(deviceImei);
//        if(bDeviceManager==null){
//            return false;
//        }else if(bDeviceManager.getDeviceStatus()==null) {
//            return false;
//        }else if(bDeviceManager.getDeviceStatus()==1||bDeviceManager.getDeviceStatus()==2){
//            return true;
//        }else{
//            return false;
//        }
        return true;
    }

    @Override
    public BDeviceManager exportDevice(BDeviceManagerJson bDeviceManagerJson) {
        BDeviceManager deviceManager = bDeviceManagerMapper.selectBDeviceManagerByImei(bDeviceManagerJson.getDeviceMac());
        // 设备表
        // bDeviceManagerJson.setBDeviceManager(deviceManager);
//        //通讯口配置
//        BDeviceComConfig bDeviceComConfig=new BDeviceComConfig();
//        bDeviceComConfig.setDeviceImei(bDeviceManagerJson.getDeviceMac());
//        List<BDeviceComConfig> bDeviceComConfigs = bDeviceComConfigService.selectBDeviceComConfigList(bDeviceComConfig);
//        bDeviceManagerJson.setBDeviceComConfigs(bDeviceComConfigs);
//        //驱动
//        BDeviceDrive bDeviceDrive=new BDeviceDrive();
//        bDeviceDrive.setDeviceImei(bDeviceManagerJson.getDeviceMac());
//        List<BDeviceDrive> bDeviceDrives = bDeviceDriveService.selectBDeviceDriveList(bDeviceDrive);
//        bDeviceManagerJson.setBDeviceDrives(bDeviceDrives);
//        //变量
//        BDeviceRealData bDeviceRealData=new BDeviceRealData();
//        bDeviceRealData.setDeviceImei(bDeviceManagerJson.getDeviceMac());
//        List<BDeviceRealData> bDeviceRealData1s = bDeviceRealDataService.selectBDeviceRealDataList(bDeviceRealData);
//        bDeviceManagerJson.setBDeviceRealDatas(bDeviceRealData1s);
//        //映射表
//        BDeviceParameter bDeviceParameter=new BDeviceParameter();
//        bDeviceParameter.setRealDataGuidLike(bDeviceManagerJson.getDeviceMac());
//        bDeviceManagerJson.setBDeviceParameters(bDeviceParameterService.selectBDeviceParameterList(bDeviceParameter));
//        //报警配置表
//        BDeviceWarnConfig bDeviceWarnConfig=new BDeviceWarnConfig();
//        bDeviceWarnConfig.setRealDataGuidLike(bDeviceManagerJson.getDeviceMac());
//        bDeviceManagerJson.setBDeviceWarnConfigs(bDeviceWarnConfigService.selectBDeviceWarnConfigList(bDeviceWarnConfig));
//        //组态
//        BDeviceZt bDeviceZt=new BDeviceZt();
//        bDeviceZt.setDeviceMac(bDeviceManagerJson.getDeviceMac());
//        List<BDeviceZt> bDeviceZts = bDeviceZtService.selectBDeviceZtList(bDeviceZt);
//        bDeviceManagerJson.setBDeviceZts(bDeviceZts);
//        //组态管理表
//        BDeviceZtBind bDeviceZtBind=new BDeviceZtBind();
//        bDeviceZtBind.setDeviceMac(bDeviceManagerJson.getDeviceMac());
//        List<BDeviceZtBind> bDeviceZtBinds = bDeviceZtBindService.selectBDeviceZtBindList(bDeviceZtBind);
//        bDeviceManagerJson.setBDeviceZtBinds(bDeviceZtBinds);
//        //短信通知
//        BDevicePhone bDevicePhone=new BDevicePhone();
//        bDevicePhone.setDeviceMac(bDeviceManagerJson.getDeviceMac());
//        List<BDevicePhone> bDevicePhones = bDevicePhoneService.selectBDevicePhoneList(bDevicePhone);
//        bDeviceManagerJson.setBDevicePhones(bDevicePhones);
        return deviceManager;
    }

    @Override
    public void copyDevice(BDeviceManagerJson bDeviceManagerJson) {
        // 设备表
        BDeviceManager deviceManager = bDeviceManagerMapper.selectBDeviceManagerByImei(bDeviceManagerJson.getDeviceMac());
        BDeviceManager bDeviceManagerNew = EntityUtil.mapping(deviceManager, BDeviceManager.class);
        bDeviceManagerNew.setUpdateBy(SecurityUtils.getUsername());
        bDeviceManagerNew.setUpdateTime(new Date());
        bDeviceManagerNew.setId(null);
        bDeviceManagerNew.setReportTime(null);
        bDeviceManagerNew.setCreateBy(SecurityUtils.getUsername());
        bDeviceManagerNew.setCreateTime(new Date());
        bDeviceManagerNew.setDeviceMac(bDeviceManagerJson.getDeviceMacNew());
        bDeviceManagerNew.setDeviceStatus(3);
        bDeviceManagerNew.setDeviceName(bDeviceManagerJson.getDeviceNameNew());
        bDeviceManagerNew.setGuid(UUID.randomUUID().toString());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String deptIds = "," + loginUser.getUser().getDept().getAncestors() + "," + loginUser
                .getUser()
                .getDeptId() + ",";
        bDeviceManagerNew.setDeptIdStrs(deptIds);
        bDeviceManagerMapper.insertBDeviceManager(bDeviceManagerNew);
        // 通讯口配置
        BDeviceComConfig bDeviceComConfig = new BDeviceComConfig();
        bDeviceComConfig.setDeviceImei(bDeviceManagerJson.getDeviceMac());
        List<BDeviceComConfig> bDeviceComConfigs = bDeviceComConfigService.selectBDeviceComConfigList(bDeviceComConfig);
        for (int i = 0; i < bDeviceComConfigs.size(); i++) {
            BDeviceComConfig deviceComConfig = bDeviceComConfigs.get(i);
            BDeviceComConfig bDeviceComConfigNew = EntityUtil.mapping(deviceComConfig, BDeviceComConfig.class);
            bDeviceComConfigNew.setDeviceImei(bDeviceManagerJson.getDeviceMacNew());
            bDeviceComConfigNew.setId(null);
            bDeviceComConfigService.insertBDeviceComConfig(bDeviceComConfigNew);
        }
        // 驱动
        BDeviceDrive bDeviceDrive = new BDeviceDrive();
        bDeviceDrive.setDeviceImei(bDeviceManagerJson.getDeviceMac());
        List<BDeviceDrive> bDeviceDrives = bDeviceDriveService.selectBDeviceDriveList(bDeviceDrive);
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < bDeviceDrives.size(); i++) {
            BDeviceDrive deviceDrive = bDeviceDrives.get(i);
            BDeviceDrive bDeviceDriveNew = EntityUtil.mapping(deviceDrive, BDeviceDrive.class);
            bDeviceDriveNew.setDeviceImei(bDeviceManagerJson.getDeviceMacNew());
            Snowflake snowflake = IdUtil.getSnowflake(6, 2);
            long id = snowflake.nextId();
            bDeviceDriveNew.setId(id);
            bDeviceDriveService.insertBDeviceDrive(bDeviceDriveNew);
            map.put(Convert.toStr(deviceDrive.getId()), Convert.toStr(bDeviceDriveNew.getId()));
        }
        // 变量
        BDeviceRealData bDeviceRealData = new BDeviceRealData();
        bDeviceRealData.setDeviceImei(bDeviceManagerJson.getDeviceMac());
        List<BDeviceRealData> bDeviceRealData1s = bDeviceRealDataService.selectBDeviceRealDataList(bDeviceRealData);
        for (int i = 0; i < bDeviceRealData1s.size(); i++) {
            BDeviceRealData deviceRealData = bDeviceRealData1s.get(i);
            BDeviceRealData bDeviceRealDataNew = EntityUtil.mapping(deviceRealData, BDeviceRealData.class);
            bDeviceRealDataNew.setDeviceImei(bDeviceManagerJson.getDeviceMacNew());
            bDeviceRealDataNew.setId(null);
            bDeviceRealDataNew.setDriveName(Convert.toStr(map.get(deviceRealData.getDeviceName())));
            String guid = UUID.randomUUID().toString();
            bDeviceRealDataNew.setGuid(guid);
            bDeviceRealDataService.insertBDeviceRealData(bDeviceRealDataNew);
            redisCache.setCacheObject("RealData-" + bDeviceRealDataNew.getDeviceImei() + "-" + bDeviceRealDataNew.getParamField(), bDeviceRealDataNew);
            BDeviceWarnConfig bDeviceWarnConfig = new BDeviceWarnConfig();
            bDeviceWarnConfig.setRealDataGuid(deviceRealData.getDeviceImei() + "-" + deviceRealData.getParamField());
            List<BDeviceWarnConfig> bDeviceWarnConfigs = bDeviceWarnConfigService.selectBDeviceWarnConfigList(bDeviceWarnConfig);
            for (int j = 0; j < bDeviceWarnConfigs.size(); j++) {
                BDeviceWarnConfig deviceWarnConfig = bDeviceWarnConfigs.get(j);
                BDeviceWarnConfig bDeviceWarnConfigNew = EntityUtil.mapping(deviceWarnConfig, BDeviceWarnConfig.class);
                bDeviceWarnConfigNew.setId(null);
                bDeviceWarnConfigNew.setRealDataGuid(bDeviceManagerJson.getDeviceMacNew() + "-" + deviceRealData.getParamField());
                bDeviceWarnConfigService.insertBDeviceWarnConfig(bDeviceWarnConfigNew);
            }

            BDeviceParameter bDeviceParameter = new BDeviceParameter();
            bDeviceParameter.setRealDataGuid(deviceRealData.getDeviceImei() + "-" + deviceRealData.getParamField());
            List<BDeviceParameter> bDeviceParameters = bDeviceParameterService.selectBDeviceParameterList(bDeviceParameter);
            for (int j = 0; j < bDeviceParameters.size(); j++) {
                BDeviceParameter deviceParameter = bDeviceParameters.get(j);
                BDeviceParameter bDeviceParameterNew = EntityUtil.mapping(deviceParameter, BDeviceParameter.class);
                bDeviceParameterNew.setId(null);
                bDeviceParameterNew.setRealDataGuid(bDeviceManagerJson.getDeviceMacNew() + "-" + deviceRealData.getParamField());
                bDeviceParameterService.insertBDeviceParameter(bDeviceParameterNew);
            }
        }
        // 组态
        BDeviceZt bDeviceZt = new BDeviceZt();
        bDeviceZt.setDeviceMac(bDeviceManagerJson.getDeviceMac());
        List<BDeviceZt> bDeviceZts = bDeviceZtService.selectBDeviceZtList(bDeviceZt);
        for (int i = 0; i < bDeviceZts.size(); i++) {
            BDeviceZt deviceZt = bDeviceZts.get(i);
            String guid = UUID.randomUUID().toString();
            BDeviceZt deviceZtNew = EntityUtil.mapping(deviceZt, BDeviceZt.class);
            deviceZtNew.setUpdateBy(SecurityUtils.getUsername());
            deviceZtNew.setUpdateTime(new Date());
            deviceZtNew.setId(null);
            deviceZtNew.setCreateBy(SecurityUtils.getUsername());
            deviceZtNew.setCreateTime(new Date());
            deviceZtNew.setDeviceMac(bDeviceManagerJson.getDeviceMacNew());
            deviceZtNew.setDeviceName(bDeviceManagerJson.getDeviceNameNew());
            deviceZtNew.setGuid(guid);
            bDeviceZtService.insertBDeviceZt(deviceZtNew);
            BDeviceZtBind bDeviceZtBind = new BDeviceZtBind();
            bDeviceZtBind.setDeviceMac(deviceZtNew.getDeviceMac());
            bDeviceZtBind.setZtGuid(deviceZtNew.getGuid());
            bDeviceZtBindService.insertBDeviceZtBind(bDeviceZtBind);
        }
        // 短信通知
        BDevicePhone bDevicePhone = new BDevicePhone();
        bDevicePhone.setDeviceMac(bDeviceManagerJson.getDeviceMac());
        List<BDevicePhone> bDevicePhones = bDevicePhoneService.selectBDevicePhoneList(bDevicePhone);
        for (int i = 0; i < bDevicePhones.size(); i++) {
            BDevicePhone devicePhone = bDevicePhones.get(i);
            BDevicePhone bDevicePhoneNew = EntityUtil.mapping(devicePhone, BDevicePhone.class);
            bDevicePhoneNew.setId(null);
            bDevicePhoneNew.setDeviceMac(bDeviceManagerJson.getDeviceMacNew());
            bDevicePhoneNew.setDeviceName(bDeviceManagerJson.getDeviceNameNew());
            bDevicePhoneService.insertBDevicePhone(bDevicePhoneNew);
        }
    }

    @Override
    public void deleteDevice(BDeviceManager bDeviceManager) {
        if (Detect.isEmpty(bDeviceManager.getDeviceMac())) {
            return;
        }
        String sql = "delete from b_device_real_data where device_imei=?";
        jdbcTemplate.update(sql, bDeviceManager.getDeviceMac());
        sql = "delete from b_device_warn_config where real_data_guid like ?";
        jdbcTemplate.update(sql, "%" + bDeviceManager.getDeviceMac() + "%");
        sql = "delete from b_device_parameter where real_data_guid like ?";
        jdbcTemplate.update(sql, "%" + bDeviceManager.getDeviceMac() + "%");
        sql = "delete from b_device_zt where device_mac=?";
        jdbcTemplate.update(sql, bDeviceManager.getDeviceMac());
        sql = "delete from b_device_zt_bind where device_mac=?";
        jdbcTemplate.update(sql, bDeviceManager.getDeviceMac());
        sql = "delete from b_device_phone where device_mac=?";
        jdbcTemplate.update(sql, bDeviceManager.getDeviceMac());
    }

    @Override
    public BDeviceStatistic getDeviceStatistic(BDeviceStatistic bDeviceManager) {
        BDeviceStatistic bDeviceMonitorVo = new BDeviceStatistic();
        // 设备总数
        Integer deviceSumNumber = 0;
        // 在线总数
        Integer onLineNumber = 0;
        // 掉线总数
        Integer offLineNumber = 0;
        // 报警总数
        Integer warnNumber = 0;

        // 报警总个数
        Long warnRecordNum = 0L;
        // 报警已处理个数
        Long warnRecordProcessedNum = 0L;
        // 报警未处理个数
        Long warnRecordUntreatedNum = 0L;

        // 工单总个数
        Long orderRecordNum = 0L;
        // 工单已处理个数
        Long orderRecordProcessedNum = 0L;
        // 工单未处理个数
        Long orderRecordUntreatedNum = 0L;
        // 工单已废弃个数
        Long orderRecordAbandonNum = 0L;
        if (Detect.isEmpty(bDeviceManager.getQueryDate())) {
            bDeviceManager.setQueryDate(DateUtil.format(new Date(), "yyyy-MM-dd"));
        }
        if (bDeviceManager.getQueryMonth() == null) {
            Date parseDate = DateUtilLocal.parseDate(bDeviceManager.getQueryDate());
            String monthStr = "";
            if (DateUtil.month(parseDate) + 1 < 10) {
                monthStr = "0" + (DateUtil.month(parseDate) + 1);
            } else {
                monthStr = "" + (DateUtil.month(parseDate) + 1);
            }
            bDeviceManager.setQueryMonth(DateUtil.year(parseDate) + "-" + monthStr);
        }
        if (Detect.notEmpty(bDeviceManager.getZtGuid())) {
            String ztSql = "select * from b_device_zt_bind where zt_guid=?";
            BeanPropertyRowMapper<BDeviceZtBind> rowMapper = new BeanPropertyRowMapper<>(BDeviceZtBind.class);
            List<BDeviceZtBind> ztBinds = jdbcTemplate.query(ztSql, rowMapper, bDeviceManager.getZtGuid());
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < ztBinds.size(); i++) {
                stringBuffer.append("'");
                stringBuffer.append(ztBinds.get(i).getDeviceMac());
                stringBuffer.append("'");
                stringBuffer.append(",");
            }
            bDeviceManager.setDeviceImei(stringBuffer.substring(0, stringBuffer.toString().length() - 1));
        }
        if ("设备总数".equals(bDeviceManager.getQueryType())) {
            deviceSumNumber = jdbcTemplate.queryForObject("select count(1) from B_Device_Manager where dept_Id_strs like ?", Integer.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(deviceSumNumber));
        } else if ("设备在线数".equals(bDeviceManager.getQueryType())) {
            onLineNumber = jdbcTemplate.queryForObject("select count(1) from B_Device_Manager where device_Status=1  and  dept_Id_strs like ?", Integer.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(onLineNumber));
        } else if ("设备离线数".equals(bDeviceManager.getQueryType())) {
            offLineNumber = jdbcTemplate.queryForObject("select count(1) from B_Device_Manager where device_Status=0  and  dept_Id_strs like ?", Integer.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(offLineNumber));
        } else if ("设备报警数".equals(bDeviceManager.getQueryType())) {
            warnNumber = jdbcTemplate.queryForObject("select count(1) from B_Device_Manager where device_Status=2  and  dept_Id_strs like ?", Integer.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(warnNumber));
        } else if ("在线比率".equals(bDeviceManager.getQueryType())) {
            deviceSumNumber = jdbcTemplate.queryForObject("select count(1) from B_Device_Manager where dept_Id_strs like ?", Integer.class, bDeviceManager.getDeptIdStrs());
            onLineNumber = jdbcTemplate.queryForObject("select count(1) from B_Device_Manager where device_Status=1  and  dept_Id_strs like ?", Integer.class, bDeviceManager.getDeptIdStrs());
            if (deviceSumNumber != 0) {
                bDeviceMonitorVo.setReturnCount(NumberUtil
                                                        .round((double) onLineNumber / deviceSumNumber * 100, 2)
                                                        .doubleValue() + "%");
            } else {
                bDeviceMonitorVo.setReturnCount("0%");
            }
        } else if ("日报警总数".equals(bDeviceManager.getQueryType())) {
            warnRecordNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where  TO_DAYS(warn_time)=TO_DAYS(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(warnRecordNum));
        } else if ("日报警已处理".equals(bDeviceManager.getQueryType())) {
            warnRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where process_state=1 and TO_DAYS(warn_time)=TO_DAYS(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(warnRecordProcessedNum));
        } else if ("日报警未处理".equals(bDeviceManager.getQueryType())) {
            warnRecordUntreatedNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where process_state=0 and TO_DAYS(warn_time)=TO_DAYS(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(warnRecordUntreatedNum));
        } else if ("日报警处理比率".equals(bDeviceManager.getQueryType())) {
            warnRecordNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where  TO_DAYS(warn_time)=TO_DAYS(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            warnRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where process_state=1 and TO_DAYS(warn_time)=TO_DAYS(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            if (warnRecordNum != 0) {
                bDeviceMonitorVo.setReturnCount(NumberUtil
                                                        .round((double) warnRecordProcessedNum / warnRecordNum * 100, 2)
                                                        .doubleValue() + "%");
            } else {
                bDeviceMonitorVo.setReturnCount("0%");
            }
        } else if ("周报警总数".equals(bDeviceManager.getQueryType())) {
            warnRecordNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where  week(warn_time)=week(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(warnRecordNum));
        } else if ("周报警已处理".equals(bDeviceManager.getQueryType())) {
            warnRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where process_state=1 and week(warn_time)=week(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(warnRecordProcessedNum));
        } else if ("周报警未处理".equals(bDeviceManager.getQueryType())) {
            warnRecordUntreatedNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where process_state=0 and week(warn_time)=week(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(warnRecordUntreatedNum));
        } else if ("周报警处理比率".equals(bDeviceManager.getQueryType())) {
            warnRecordNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where  week(warn_time)=week(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            warnRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where process_state=1 and week(warn_time)=week(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            if (warnRecordNum != 0) {
                bDeviceMonitorVo.setReturnCount(NumberUtil
                                                        .round((double) warnRecordProcessedNum / warnRecordNum * 100, 2)
                                                        .doubleValue() + "%");
            } else {
                bDeviceMonitorVo.setReturnCount("0%");
            }
        } else if ("月报警总数".equals(bDeviceManager.getQueryType())) {
            warnRecordNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where  MONTH(warn_time)=MONTH(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(warnRecordNum));
        } else if ("月报警已处理".equals(bDeviceManager.getQueryType())) {
            warnRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where process_state=1 and MONTH(warn_time)=MONTH(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(warnRecordProcessedNum));
        } else if ("月报警未处理".equals(bDeviceManager.getQueryType())) {
            warnRecordUntreatedNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where process_state=0 and MONTH(warn_time)=MONTH(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(warnRecordUntreatedNum));
        } else if ("月报警处理比率".equals(bDeviceManager.getQueryType())) {
            warnRecordNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where  MONTH(warn_time)=MONTH(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            warnRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where process_state=1 and MONTH(warn_time)=MONTH(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            if (warnRecordNum != 0) {
                bDeviceMonitorVo.setReturnCount(NumberUtil
                                                        .round((double) warnRecordProcessedNum / warnRecordNum * 100, 2)
                                                        .doubleValue() + "%");
            } else {
                bDeviceMonitorVo.setReturnCount("0%");
            }
        } else if ("年报警总数".equals(bDeviceManager.getQueryType())) {
            warnRecordNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where  year(warn_time)=year(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(warnRecordNum));
        } else if ("年报警已处理".equals(bDeviceManager.getQueryType())) {
            warnRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where process_state=1 and year(warn_time)=year(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(warnRecordProcessedNum));
        } else if ("年报警未处理".equals(bDeviceManager.getQueryType())) {
            warnRecordUntreatedNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where process_state=0 and year(warn_time)=year(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(warnRecordUntreatedNum));
        } else if ("年报警处理比率".equals(bDeviceManager.getQueryType())) {
            warnRecordNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where  year(warn_time)=year(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            warnRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_device_warn where process_state=1 and year(warn_time)=year(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            if (warnRecordNum != 0) {
                bDeviceMonitorVo.setReturnCount(NumberUtil
                                                        .round((double) warnRecordProcessedNum / warnRecordNum * 100, 2)
                                                        .doubleValue() + "%");
            } else {
                bDeviceMonitorVo.setReturnCount("0%");
            }
        } else if ("日工单总数".equals(bDeviceManager.getQueryType())) {
            orderRecordNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where TO_DAYS(create_time)=TO_DAYS(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(orderRecordNum));
        } else if ("日工单已处理".equals(bDeviceManager.getQueryType())) {
            orderRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=2 and TO_DAYS(create_time)=TO_DAYS(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(orderRecordProcessedNum));
        } else if ("日工单未处理".equals(bDeviceManager.getQueryType())) {
            orderRecordUntreatedNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=0 and TO_DAYS(create_time)=TO_DAYS(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(orderRecordUntreatedNum));
        } else if ("日工单处理比率".equals(bDeviceManager.getQueryType())) {
            orderRecordNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where TO_DAYS(create_time)=TO_DAYS(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            orderRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=2 and TO_DAYS(create_time)=TO_DAYS(NOW()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            if (orderRecordNum != 0) {
                bDeviceMonitorVo.setReturnCount(NumberUtil
                                                        .round((double) orderRecordProcessedNum / orderRecordNum * 100, 2)
                                                        .doubleValue() + "%");
            } else {
                bDeviceMonitorVo.setReturnCount("0%");
            }
        } else if ("周工单总数".equals(bDeviceManager.getQueryType())) {
            orderRecordNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where week(create_time) =week(Now()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(orderRecordNum));
        } else if ("周工单已处理".equals(bDeviceManager.getQueryType())) {
            orderRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=2 and week(create_time) =week(Now()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(orderRecordProcessedNum));
        } else if ("周工单未处理".equals(bDeviceManager.getQueryType())) {
            orderRecordUntreatedNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=0 and week(create_time) =week(Now()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(orderRecordUntreatedNum));
        } else if ("周工单处理比率".equals(bDeviceManager.getQueryType())) {
            orderRecordNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where week(create_time) =week(Now()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            orderRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=2 and week(create_time) =week(Now()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            if (orderRecordNum != 0) {
                bDeviceMonitorVo.setReturnCount(NumberUtil
                                                        .round((double) orderRecordProcessedNum / orderRecordNum * 100, 2)
                                                        .doubleValue() + "%");
            } else {
                bDeviceMonitorVo.setReturnCount("0%");
            }
        } else if ("月工单总数".equals(bDeviceManager.getQueryType())) {
            orderRecordNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where MONTH(create_time) =MONTH(Now()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(orderRecordNum));
        } else if ("月工单已处理".equals(bDeviceManager.getQueryType())) {
            orderRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=2 and MONTH(create_time) =MONTH(Now()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(orderRecordProcessedNum));
        } else if ("月工单未处理".equals(bDeviceManager.getQueryType())) {
            orderRecordUntreatedNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=0 and MONTH(create_time) =MONTH(Now()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(orderRecordUntreatedNum));
        } else if ("月工单处理比率".equals(bDeviceManager.getQueryType())) {
            orderRecordNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where MONTH(create_time) =MONTH(Now()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            orderRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=2 and MONTH(create_time) =MONTH(Now()) and dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            if (orderRecordNum != 0) {
                bDeviceMonitorVo.setReturnCount(NumberUtil
                                                        .round((double) orderRecordProcessedNum / orderRecordNum * 100, 2)
                                                        .doubleValue() + "%");
            } else {
                bDeviceMonitorVo.setReturnCount("0%");
            }
        } else if ("年工单总数".equals(bDeviceManager.getQueryType())) {
            orderRecordNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where year(create_time) =year(Now()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(orderRecordNum));
        } else if ("年工单已处理".equals(bDeviceManager.getQueryType())) {
            orderRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=2 and year(create_time) =year(Now()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(orderRecordProcessedNum));
        } else if ("年工单未处理".equals(bDeviceManager.getQueryType())) {
            orderRecordUntreatedNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=0 and year(create_time) =year(Now()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            bDeviceMonitorVo.setReturnCount(Convert.toStr(orderRecordUntreatedNum));
        } else if ("年工单处理比率".equals(bDeviceManager.getQueryType())) {
            orderRecordNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where year(create_time) =year(Now()) and  dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            orderRecordProcessedNum = jdbcTemplate.queryForObject("select count(1) from b_work_order where process_state=2 and year(create_time) =year(Now()) and dept_Id_strs like ?", Long.class, bDeviceManager.getDeptIdStrs());
            if (orderRecordNum != 0) {
                bDeviceMonitorVo.setReturnCount(NumberUtil
                                                        .round((double) orderRecordProcessedNum / orderRecordNum * 100, 2)
                                                        .doubleValue() + "%");
            } else {
                bDeviceMonitorVo.setReturnCount("0%");
            }
        } else if ("日累加".equals(bDeviceManager.getQueryType())) {
            if (Detect.notEmpty(bDeviceManager.getParamNames())) {
                String[] split = bDeviceManager.getParamNames().split(",");
                StringBuffer stringBuffer = new StringBuffer();
                for (int i = 0; i < split.length; i++) {
                    stringBuffer.append("'");
                    stringBuffer.append(split[i]);
                    stringBuffer.append("'");
                    stringBuffer.append(",");
                }
                bDeviceManager.setParamNames(stringBuffer.substring(0, stringBuffer.toString().length() - 1));
                String sql = "SELECT sum(param_value) from b_device_history_data where device_imei in({}) and param_name in({}) \n" + "and param_value !='bad' and date_format(report_time,'%Y-%m-%d') = '{}'";
                String format = StrUtil.format(sql, bDeviceManager.getDeviceImei(), bDeviceManager.getParamNames(), bDeviceManager.getQueryDate());
                Long returnCount = jdbcTemplate.queryForObject(format, Long.class);
                bDeviceMonitorVo.setReturnCount(Convert.toStr(returnCount));
            } else {
                bDeviceMonitorVo.setReturnCount("0");
            }
        } else if ("日累减".equals(bDeviceManager.getQueryType())) {
            if (Detect.notEmpty(bDeviceManager.getParamNames())) {
                String[] split = bDeviceManager.getParamNames().split(",");
                StringBuffer stringBuffer = new StringBuffer();
                for (int i = 0; i < split.length; i++) {
                    stringBuffer.append("'");
                    stringBuffer.append(split[i]);
                    stringBuffer.append("'");
                    stringBuffer.append(",");
                }
                bDeviceManager.setParamNames(stringBuffer.substring(0, stringBuffer.toString().length() - 1));
                String sql = "SELECT max(param_value) from b_device_history_data where device_imei in({}) and param_name in({}) \n" + "and param_value !='bad' and TO_DAYS(report_time)=TO_DAYS(NOW())";
                String format = StrUtil.format(sql, bDeviceManager.getDeviceImei(), bDeviceManager.getParamNames());
                Long maxCount = Convert.toLong(jdbcTemplate.queryForObject(format, Long.class), 0L);
                sql = "SELECT min(param_value) from b_device_history_data where device_imei='{}' and param_name in({}) \n" + "and param_value !='bad' and date_format(report_time,'%Y-%m-%d') = '{}'";
                format = StrUtil.format(sql, bDeviceManager.getDeviceImei(), bDeviceManager.getParamNames(), bDeviceManager.getQueryDate());
                Long minCount = Convert.toLong(jdbcTemplate.queryForObject(format, Long.class), 0L);
                bDeviceMonitorVo.setReturnCount(Convert.toStr(maxCount - minCount));
            } else {
                bDeviceMonitorVo.setReturnCount("0");
            }
        } else if ("日平均".equals(bDeviceManager.getQueryType())) {
            if (Detect.notEmpty(bDeviceManager.getParamNames())) {
                String[] split = bDeviceManager.getParamNames().split(",");
                StringBuffer stringBuffer = new StringBuffer();
                for (int i = 0; i < split.length; i++) {
                    stringBuffer.append("'");
                    stringBuffer.append(split[i]);
                    stringBuffer.append("'");
                    stringBuffer.append(",");
                }
                bDeviceManager.setParamNames(stringBuffer.substring(0, stringBuffer.toString().length() - 1));
                String sql = "SELECT avg(param_value) from b_device_history_data where device_imei in({}) and param_name in({}) \n" + "and param_value !='bad' and date_format(report_time,'%Y-%m-%d') = '{}'";
                String format = StrUtil.format(sql, bDeviceManager.getDeviceImei(), bDeviceManager.getParamNames(), bDeviceManager.getQueryDate());
                Double avgCount = Convert.toDouble(jdbcTemplate.queryForObject(format, Double.class), 0.0);
                bDeviceMonitorVo.setReturnCount(NumberUtil.roundStr(Convert.toStr(avgCount), 2));
            } else {
                bDeviceMonitorVo.setReturnCount("0");
            }
        } else if ("月累加".equals(bDeviceManager.getQueryType())) {
            if (Detect.notEmpty(bDeviceManager.getParamNames())) {
                String[] split = bDeviceManager.getParamNames().split(",");
                StringBuffer stringBuffer = new StringBuffer();
                for (int i = 0; i < split.length; i++) {
                    stringBuffer.append("'");
                    stringBuffer.append(split[i]);
                    stringBuffer.append("'");
                    stringBuffer.append(",");
                }
                bDeviceManager.setParamNames(stringBuffer.substring(0, stringBuffer.toString().length() - 1));
                String sql = "SELECT sum(param_value) from b_device_history_data where device_imei in({}) and param_name in({}) \n" + "and param_value !='bad' and DATE_FORMAT(report_time,'%Y-%m')= '{}'";
                String format = StrUtil.format(sql, bDeviceManager.getDeviceImei(), bDeviceManager.getParamNames(), bDeviceManager.getQueryMonth());
                Long returnCount = jdbcTemplate.queryForObject(format, Long.class);
                bDeviceMonitorVo.setReturnCount(Convert.toStr(returnCount));
            } else {
                bDeviceMonitorVo.setReturnCount("0");
            }
        } else if ("月累减".equals(bDeviceManager.getQueryType())) {
            if (Detect.notEmpty(bDeviceManager.getParamNames())) {
                String[] split = bDeviceManager.getParamNames().split(",");
                StringBuffer stringBuffer = new StringBuffer();
                for (int i = 0; i < split.length; i++) {
                    stringBuffer.append("'");
                    stringBuffer.append(split[i]);
                    stringBuffer.append("'");
                    stringBuffer.append(",");
                }
                bDeviceManager.setParamNames(stringBuffer.substring(0, stringBuffer.toString().length() - 1));
                String sql = "SELECT max(param_value) from b_device_history_data where device_imei in({}) and param_name in({}) \n" + "and param_value !='bad' and DATE_FORMAT(report_time,'%Y-%m')= '{}'";
                String format = StrUtil.format(sql, bDeviceManager.getDeviceImei(), bDeviceManager.getParamNames(), bDeviceManager.getQueryMonth());
                Long maxCount = Convert.toLong(jdbcTemplate.queryForObject(format, Long.class), 0L);
                sql = "SELECT min(param_value) from b_device_history_data where device_imei in({}) and param_name in({}) \n" + "and param_value !='bad' and DATE_FORMAT(report_time,'%Y-%m')= '{}'";
                format = StrUtil.format(sql, bDeviceManager.getDeviceImei(), bDeviceManager.getParamNames(), bDeviceManager.getQueryMonth());
                Long minCount = Convert.toLong(jdbcTemplate.queryForObject(format, Long.class), 0L);
                bDeviceMonitorVo.setReturnCount(Convert.toStr(maxCount - minCount));
            } else {
                bDeviceMonitorVo.setReturnCount("0");
            }
        } else if ("月平均".equals(bDeviceManager.getQueryType())) {
            if (Detect.notEmpty(bDeviceManager.getParamNames())) {
                String[] split = bDeviceManager.getParamNames().split(",");
                StringBuffer stringBuffer = new StringBuffer();
                for (int i = 0; i < split.length; i++) {
                    stringBuffer.append("'");
                    stringBuffer.append(split[i]);
                    stringBuffer.append("'");
                    stringBuffer.append(",");
                }
                bDeviceManager.setParamNames(stringBuffer.substring(0, stringBuffer.toString().length() - 1));
                String sql = "SELECT avg() from b_device_history_data where device_imei in({}) and param_name in({}) \n" + "and param_value !='bad' and DATE_FORMAT(report_time,'%Y-%m')= '{}' ";
                String format = StrUtil.format(sql, bDeviceManager.getDeviceImei(), bDeviceManager.getParamNames(), bDeviceManager.getQueryMonth());
                Double avgCount = Convert.toDouble(jdbcTemplate.queryForObject(format, Double.class), 0.0);
                bDeviceMonitorVo.setReturnCount(NumberUtil.roundStr(Convert.toStr(avgCount), 2));
            } else {
                bDeviceMonitorVo.setReturnCount("0");
            }
        } else if ("累加".equals(bDeviceManager.getQueryType())) {
            if (Detect.notEmpty(bDeviceManager.getParamNames())) {
                String[] split = bDeviceManager.getParamNames().split(",");
                Long returnCount = 0L;
                for (int i = 0; i < split.length; i++) {
                    String sql = "SELECT param_value from b_device_history_data where device_imei in({}) and param_name ='{}' \n" + "and param_value !='bad' ORDER BY id desc limit 1";
                    String format = StrUtil.format(sql, bDeviceManager.getDeviceImei(), split[i]);
                    returnCount = jdbcTemplate.queryForObject(format, Long.class) + returnCount;
                }
                bDeviceMonitorVo.setReturnCount(Convert.toStr(returnCount));
            } else {
                bDeviceMonitorVo.setReturnCount("0");
            }
        } else if ("累减".equals(bDeviceManager.getQueryType())) {
            if (Detect.notEmpty(bDeviceManager.getParamNames())) {
                String[] split = bDeviceManager.getParamNames().split(",");
                Long initCount = 0L;
                Long returnCount = 0L;
                for (int i = 0; i < split.length; i++) {
                    String sql = "SELECT param_value from b_device_history_data where device_imei in({}) and param_name ='{}' \n" + "and param_value !='bad' ORDER BY id desc limit 1";
                    String format = StrUtil.format(sql, bDeviceManager.getDeviceImei(), split[i]);
                    if (i == 0) {
                        initCount = jdbcTemplate.queryForObject(format, Long.class);
                    } else {
                        returnCount = initCount - jdbcTemplate.queryForObject(format, Long.class);
                    }
                }
                bDeviceMonitorVo.setReturnCount(Convert.toStr(returnCount));
            } else {
                bDeviceMonitorVo.setReturnCount("0");
            }
        } else if ("累平均".equals(bDeviceManager.getQueryType())) {
            if (Detect.notEmpty(bDeviceManager.getParamNames())) {
                String[] split = bDeviceManager.getParamNames().split(",");
                Long returnCount = 0L;
                for (int i = 0; i < split.length; i++) {
                    String sql = "SELECT param_value from b_device_history_data where device_imei in({}) and param_name ='{}' \n" + "and param_value !='bad' ORDER BY id desc limit 1";
                    String format = StrUtil.format(sql, bDeviceManager.getDeviceImei(), split[i]);
                    returnCount = jdbcTemplate.queryForObject(format, Long.class) + returnCount;
                }
                Double avgCount = Convert.toDouble(returnCount / split.length, 0.0);
                bDeviceMonitorVo.setReturnCount(Convert.toStr(avgCount));
            } else {
                bDeviceMonitorVo.setReturnCount("0");
            }
        }
        return bDeviceMonitorVo;
    }

    @Override
    public Integer getDeviceWarnStatus(String imei) {
        Integer deviceStatus = 1;
        Collection<String> keys = redisCache.keys("DeviceWarn-" + imei + "-*");
        for (String key : keys) {
            Object object = redisCache.getCacheObject(key);
            if (Convert.toInt(object, 1) == 2) {
                deviceStatus = 2;
            }
        }
        return deviceStatus;
    }

    private String getEchartsSql(List<String> analogList, String deviceMac, String queryTime) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("SELECT DATE_FORMAT( update_time, '%H' ) as timeNum ,");
        for (int i = 0; i < analogList.size(); i++) {
            String anaName = analogList.get(i);
            stringBuilder.append("sum(analog_" + anaName + ")/COUNT(analog_" + anaName + ") as avgData" + anaName);
            if (i < analogList.size() - 1) {
                stringBuilder.append(",");
            }
        }
        stringBuilder.append(" FROM b_device_history where device_Mac='" + deviceMac + "' and TO_DAYS(update_time)=TO_DAYS('" + queryTime + "') GROUP BY timeNum ORDER BY timeNum");
        return stringBuilder.toString();
    }

}
